package com.itany.controller;


import com.github.pagehelper.PageInfo;
import com.itany.constant.StatusConst;
import com.itany.pojo.BookWithBLOBs;
import com.itany.pojo.ManagerUser;
import com.itany.service.BookService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;


    @RequestMapping("/examineFindAll")
    public Map<String, Object> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows,
            BookWithBLOBs book,
            Integer supplierId) {
        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<BookWithBLOBs> info = bookService.selectExample(page, rows, book.getBookname(), book.getIsbn(), book.getPressid(), supplierId);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }

    @RequestMapping("/findByIsbn")
    public ActionResult findByIsbn(String isbn) {
        ActionResult result = new ActionResult();
        try {
            BookWithBLOBs book = bookService.findIsbn(isbn);
            result.setData(book);
        } catch (Exception e) {
            result.setStatus(StatusConst.FAIL_RESPONSE);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/findOne")
    public ActionResult findOne(Integer id) {
        ActionResult result = new ActionResult();
        try {
            BookWithBLOBs book = bookService.selectById(id);
        } catch (Exception e) {
            result.setStatus(StatusConst.FAIL_RESPONSE);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/update")
    public ActionResult updateFlag(BookWithBLOBs book,
                                   @RequestParam(value = "img", required = false) CommonsMultipartFile file,
                                   HttpSession session) {

        ActionResult result = new ActionResult();
        try {
            bookService.update(book);
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            result.setStatus(StatusConst.FAIL_RESPONSE);
        }
        return result;
    }

    @RequestMapping("supplerFindAll")
    public Map<String, Object> supplerFindAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows,
            String bookname,
            String isbn,
            HttpSession session) {
        ManagerUser managerUser = (ManagerUser) session.getAttribute(StatusConst.LOGIN_USER);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            if (managerUser == null) {
                throw new Exception("没登陆呢");
            }
            PageInfo<BookWithBLOBs> info = bookService.supplierFindAll(page, rows, managerUser.getSupplierid(), bookname, isbn);
            map.put("total", info.getTotal());
            map.put("rows", info.getList());
        } catch (Exception e) {
            map.put("msg", e.getMessage());
        }
        return map;


    }

}
